﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;

namespace PDO.UI.Core
{

    [ValueConversion(typeof(bool), typeof(Visibility))]
    public class VisiblityValueConverter : IValueConverter
    {
        #region IValueConverter Members

        /// <summary>
        /// This converts a boolean to a System.Windows.Visibility value.
        /// </summary>
        /// <param name="value">Value of the boolean property being bound to</param>
        /// <param name="parameter">Hidden or Collapsed depending on which return visibility is wanted</param>
        /// <returns>Visibile if the boolean property is true.  Hidden or Collapsed if it is false.
        /// Hidden or Collapsed is determined by the parameter input.</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // Initialize outVis to default non-visible value.
            Visibility outVis = Visibility.Hidden;

            // Check for alternative non-visibile value (e.g. Visibility.Collapsed) passed in parameter.
            if (parameter != null)
            {
                outVis = (Visibility)Enum.Parse(typeof(Visibility), parameter.ToString(), true);                
            }            

            try
            {
                bool converted = Boolean.Parse(value.ToString());
                if (converted)
                {
                    outVis = Visibility.Visible;
                }
            }
            catch (Exception ex)
            {
               // throw;
            }

            return outVis;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }

        #endregion
    }

}
